package com.example.steveaoblogdemo.interceptor;

import com.example.steveaoblogdemo.utils.JwtUtil;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //在请求头中取到token令牌;
        String token = request.getHeader("token");
        //验证token;
        Boolean b = JwtUtil.checkToken(token);
        //若令牌不符合则响应信息为 401;
        if(!b){
            response.getWriter().print(401);
        }
        return b;
    }
}
